﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetFwTypeLib;

namespace MyWcfRestLib
{
    /// <summary>
    /// map to NET_FW_IP_PROTOCOL
    /// </summary>
    public enum FirewallProtocol
    {
        tcp = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP,
        udp = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP
    }

    /// <summary>
    /// Helper class for add exception into windows firewall to make our service can works with corresponding ports.
    /// </summary>
    public class WindowsFirewallHelper
    {

        public static void AddPort(string name, int port, FirewallProtocol protocol)
        {
            // create firewall class instance
            INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
            INetFwOpenPort objPort = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwOpenPort"));

            objPort.Name = name;
            objPort.Port = port;
            objPort.Protocol = (NET_FW_IP_PROTOCOL_)protocol;
            objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
            objPort.Enabled = true;

            bool exist = false;
            foreach (INetFwOpenPort mPort in netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts)
            {
                if (objPort == mPort)
                {
                    exist = true;
                    break;
                }
            }
            if (!exist) netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort);
        }

        public static void AddApp(string name, string executablePath)
        {
            INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
            INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));

            app.Name = name;
            app.ProcessImageFileName = executablePath;
            app.Enabled = true;
            netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);

            bool exist = false;
            foreach (INetFwAuthorizedApplication mApp in netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications)
            {
                if (app == mApp)
                {
                    exist = true;
                    break;
                }
            }
            if (!exist) netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
        }

        public static void DeletePort(int port, FirewallProtocol protocol)
        {
            INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
            netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, (NET_FW_IP_PROTOCOL_)protocol);
        }

        public static void DeleteApp(string executablePath)
        {
            INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
            netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(executablePath);
        }
    }


}
